package com.example.demoday.controller;
import com.example.demoday.pojo.User;
import com.example.demoday.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 添加
    @PostMapping
    public String add(User user) {
        userService.add(user);

        return "添加成功";
    }

    // 删除
    @DeleteMapping
    public String delete(int id) {

        System.out.println(id);
        userService.del(id);

        return "删除成功";
    }

    // 更新
    @PutMapping
    public String update(User user) {
        userService.update(user);

        return "编辑成功";
    }

    // 查询
    @GetMapping
    public User detail(int id) {
        User user = userService.detail(id);

        return user;
    }

    // 姓名查询
    @GetMapping("/name")
    public List<User> findByName(@RequestParam String name) {
        return userService.findByName(name);
    }

    // 邮箱模糊查询
    @GetMapping("/email")
    public List<User> findByEmail(@RequestParam String email) {
        return userService.findByEmail(email);
    }

    // 分页查询
    @GetMapping("/all")
    public List<User> findAll(@RequestParam int offset, @RequestParam int limit) {
        return userService.findAll(offset, limit);
    }
}
